<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加角色区域 -->
      <el-row>
        <el-col :span="3">
          <el-button type="primary" @click="addRole">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表区域 -->
      <el-table :data="roleList" style="width: 100%" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 渲染一级权限 -->
            <el-row v-for="(item1, i1) in scope.row.children" :key="item1.id" :class="['bbottom', 'vcenter', i1 === 0 ? 'btop' : '']">
              <el-col :span="5">
                <el-tag type="success" closable @close="closeTag(scope.row, item1.id)">
                  {{item1.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 渲染二级权限 -->
                <el-row v-for="(item2, i2) in item1.children" :key="item2.id" :class="[i2 === 0 ? '' : 'btop']">
                  <el-col :span="6">
                    <el-tag type="warning" closable @close="closeTag(scope.row, item2.id)">
                      {{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 渲染三级标签 -->
                  <el-col :span="18">
                    <el-tag v-for="(item3) in item2.children" :key="item3.id" closable @close="closeTag(scope.row, item3.id)">
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editRole(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRole(scope.row.id)"></el-button>
            <el-tooltip effect="dark" content="分配指定权限" placement="top-start" :enterable='false'>
              <el-button size="mini" type="warning" icon="el-icon-setting" @click="setRight(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加角色对话框 -->
    <addRole ref="addRole"></addRole>
    <!-- 编辑角色对话框 -->
    <editRole ref="editRole"></editRole>
    <!-- 分配角色指定权限 -->
    <setRight ref="setRight"></setRight>
  </div>
</template>
<script>
import addRole from '../power/dialog/addRole'
import editRole from '../power/dialog/editRole'
import setRight from '../power/dialog/setRight'
export default {
  data: function () {
    return {
      // 角色列表
      roleList: []
    }
  },
  components: {
    addRole,
    editRole,
    setRight
  },
  created: function () {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    getRoleList: async function () {
      const res = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.warning('获取角色列表失败')
      }
      this.roleList = res.data
    },
    // 点击x号，剔除标签
    closeTag: async function (row, rightId) {
      const confirmResult = await this.$confirm('确认删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete(`roles/${row.id}/rights/${rightId}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除权限失败')
      }
      this.$message.success('删除成功')
      row.children = res.data
    },
    // 添加角色，弹出对话框
    addRole: function () {
      this.$refs.addRole.dialogVisible = true
    },
    // 编辑角色，弹出对话框
    editRole: function (row) {
      this.$refs.editRole.id = row.id
      this.$refs.editRole.addForm.roleName = row.roleName
      this.$refs.editRole.addForm.roleDesc = row.roleDesc
      this.$refs.editRole.dialogVisible = true
    },
    // 点击按钮，删除对应角色
    deleteRole: async function (id) {
      const confirmResult = await this.$confirm('删除此角色?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete(`roles/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除角色失败')
      }
      this.$message.success('删除成功')
      this.getRoleList()
    },
    // 点击按钮，分配指定权限
    setRight: function (row) {
      this.$refs.setRight.getRightsList(row)
      this.$refs.setRight.dialogVisible = true
    }
  }
}
</script>
<style lang="less" scoped>
.el-card {
  padding: 10px 30px 20px;
  .el-table {
    margin-top: 20px;
    .el-tag {
      margin: 8px;
    }
  }
}
.btop {
  border-top: 1px solid #f1eeee;
}
.bbottom {
  border-bottom: 1px solid #f1eeee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
